package cc.sbootd2.sbootd2web.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cc.sbootd2.sbootd2web.pojo.NewsType;
import cc.sbootd2.sbootd2web.service.NewsTypeService;
import cc.sbootd2.sbootd2web.util.Page;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/newstype")
public class NewsTypeController {

    @Resource
    private NewsTypeService newsTypeService;

    @RequestMapping("/add")
    public String add(Model model){
        List<NewsType> plist=newsTypeService.getTopNewsType();
        model.addAttribute("plist",plist);


        return "/newstype/add";
    }


//    @RequestMapping(value = "/doadd",method = RequestMethod.POST )
//    public String doAdd(Model model,String typeName,Integer parentId){
//        return "/newstype/add";
//    }
    @RequestMapping(value = "/doadd",method = RequestMethod.POST )
    public String doAdd(Model model, NewsType newsType){

        try{
            if(newsTypeService.save(newsType)>0){
                return "redirect:/newstype/add";
            }
        }catch (Exception e){
        }
        return "redirect:/newstype/add";
    }


    @RequestMapping( value = "/update")
    public String update(Model model, @RequestParam(required = true) Integer id){
        List<NewsType> plist=newsTypeService.getTopNewsType();
        NewsType newsType=newsTypeService.getNewsTypeById(id);

        model.addAttribute("plist",plist);
        model.addAttribute("newsType",newsType);
        return "/newstype/update";
    }


    @RequestMapping("/doupdate")
    public String doUpdate(Model model, NewsType newsType){
        try{
            if(newsTypeService.update(newsType)>0){
                return "redirect:/newstype/list";
            }
        }catch (Exception e){
        }
        model.addAttribute("lab","<script>alert('修改数据失败!');</script>");
        return "forward:/newstype/update?id="+newsType.getId();
    }


    @RequestMapping("/list")
    public String list(Model model, NewsType newsType){
        int totalRecordCount=newsTypeService.getRecordCount(newsType);
        Page pageTool=new Page(totalRecordCount,newsType.getCurrPageNo());
        newsType.setPageSize(pageTool.getPageSize());
        newsType.setStarNum(pageTool.getStartRow());
        List<NewsType> list=newsTypeService.getNewsTypeByPage(newsType);
        model.addAttribute("list",list);
        model.addAttribute("pageTool",pageTool);
        model.addAttribute("newsType",newsType);
        model.addAttribute("prePages",pageTool.getPrePages());
        model.addAttribute("nextPages",pageTool.getNextPages());
        List<NewsType> plist=newsTypeService.getTopNewsType();
        model.addAttribute("plist",plist);
        return "/newstype/list";

    }


    /**
     *
     * @param id
     * @return 1:成功 -1:有子类数据不能删除 0：删除失败
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(Integer id){


        if(newsTypeService.getChildNewsTypeCount(id)>0){
            return "-1";
        }


        try {
            if(newsTypeService.delete(id)>0){
                return "1";
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        return "0";
    }

    @RequestMapping(value = "/deleteall")
    @ResponseBody
    public String deleteAll(String ids){

       String[] idsStr= ids.split(",");

       List<Integer> idsList=new ArrayList<Integer>();
       for(String idstr:idsStr){
           idsList.add(Integer.parseInt(idstr));
       }




        if(newsTypeService.getChildNewsTypeCountByIds(idsList)>0){
            return "-1";
        }
        try {
            if(newsTypeService.deleteAll(idsList)>0){
                return "1";
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        return "0";
    }


}
